package io.renren.modules.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.app.constants.ListConstants;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.lucky.dao.AppUserAddrDao;
import io.renren.modules.lucky.dao.AppUserScoreDao;
import io.renren.modules.lucky.entity.AppUserAddrEntity;
import io.renren.modules.lucky.entity.AppUserScoreEntity;
import io.renren.modules.lucky.service.AppUserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("appUserAddrService")
public class AppUserAddrServiceImpl extends ServiceImpl<AppUserAddrDao, AppUserAddrEntity> implements AppUserAddrService {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private AppUserScoreDao scoreDao;

    @Autowired
    private AppUserAddrDao addrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppUserAddrEntity> page = this.page(
                new Query<AppUserAddrEntity>().getPage(params), new QueryWrapper<AppUserAddrEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<AppUserAddrEntity> queryPageForApp(String curPage, String pageSize, String userToken, String userId) {
        String id = jwtUtils.getClaimByToken(userToken).getSubject();
        if(id.equals(userId)) {
            QueryWrapper<AppUserAddrEntity> addrWrapper = new QueryWrapper<AppUserAddrEntity>();
            addrWrapper.eq("addr_flag", 1);
            addrWrapper.eq("user_id", userId);
            addrWrapper.orderByDesc("create_time");
            addrWrapper.last("limit "+curPage+","+pageSize);
            List<AppUserAddrEntity> list = baseMapper.selectList(addrWrapper);
            Assert.isNull(list, "地址为空");
            return list;
        }else{
            throw new RRException("请求用户id与token不匹配");
        }
    }

    /**
     * 增加用户地址
     * @param addrEntity
     * @return
     */
    public int addUserAddr(AppUserAddrEntity addrEntity){
        int num = baseMapper.insert(addrEntity);
        AppUserScoreEntity score = ListConstants.insertScore("addrInfo", addrEntity.getUserId());
        scoreDao.insert(score);
        return num;
    }

    /**
     * 删除用户地址
     * @param id
     * @return
     */
    public int delUserAddr(String id){
        AppUserAddrEntity addrEntity = new AppUserAddrEntity();
        addrEntity.setAddrFlag(0);
        int num = baseMapper.update(addrEntity, new QueryWrapper<AppUserAddrEntity>().eq("id", id));
        return num;
    }

    /**
     * 修改用户地址
     * @param addrEntity
     * @return
     */
    public int updUserAddr(AppUserAddrEntity addrEntity){
        int num = baseMapper.update(addrEntity, new QueryWrapper<AppUserAddrEntity>()
                .eq("id", addrEntity.getId()));
        return num;
    }

    @Override
    public AppUserAddrEntity queryAddrInfo(String add_id, String user_token) {
        AppUserAddrEntity user = baseMapper.selectOne(new QueryWrapper<AppUserAddrEntity>()
                .eq("id", add_id));
        Assert.isNull(user, "地址不存在");
        return user;
    }

    @Override
    public int upDefaultAddr(String addrId, String userId, String userToken) {
        int num = 0;
        String id = jwtUtils.getClaimByToken(userToken).getSubject();
        if (userId.equals(id)) {
            //更新默认地址
            AppUserAddrEntity entity = new AppUserAddrEntity();
            entity.setId(Integer.valueOf(addrId));
            entity.setAddrDefault(1);
            num = baseMapper.updateById(entity);
            if (num > 0) {
                addrDao.updateUnDefaultByUser(userId, addrId);
            }
        } else {
            throw new RRException("请求用户id与token不匹配");
        }
        return num;
    }

}